package Engine;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Coordinate{
	private double latitude;
	private double longitude;
	private float accuracy;
	private float bearing;
	
	public float getAccuracy() {
		return accuracy;
	}

	public void setAccuracy(float accuracy) {
		this.accuracy = accuracy;
	}

	public double getLatitude() {
		return latitude;
	}
	
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	
	public double getLongitude() {
		return longitude;
	}
	
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	
	public Coordinate()
	{
		latitude = 0;
		longitude = 0;
	}
	
	public Coordinate(double latitude, double longitude, float accuracy)
	{
		this.latitude = latitude;
		this.longitude = longitude;
		this.accuracy = accuracy;
	}
	
	public float getBearing() {
		return bearing;
	}

	public void setBearing(float bearing) {
		this.bearing = bearing;
	}

	public Coordinate(Element documentElement) {
		NodeList gps = documentElement.getElementsByTagName("GPS");
		this.latitude = Double.parseDouble(gps.item(0).getAttributes().getNamedItem("Lat").getNodeValue());
		this.longitude = Double.parseDouble(gps.item(0).getAttributes().getNamedItem("Long").getNodeValue());
		this.accuracy = Float.parseFloat(gps.item(0).getAttributes().getNamedItem("accuracy").getNodeValue());
		this.bearing = Float.parseFloat(gps.item(0).getAttributes().getNamedItem("bearing").getNodeValue());
		System.out.println("new coordinate : "+this.toString());
	}

	public boolean Equals(Coordinate c)
	{
		if(gps2m(c.latitude, c.longitude, this.latitude, this.longitude) < this.accuracy + c.accuracy)
			return true;
		else
			return false;
	}
	private double gps2m(double latitude2, double longitude2, double latitude3, double longitude3) {
	    float pk = (float) (180/3.14169);

	    double a1 = latitude2 / pk;
	    double a2 = longitude2 / pk;
	    double b1 = latitude3 / pk;
	    double b2 = longitude3 / pk;
	    
	    double t1 = Math.cos(a1)*Math.cos(a2)*Math.cos(b1)*Math.cos(b2);
	    double t2 = Math.cos(a1)*Math.sin(a2)*Math.cos(b1)*Math.sin(b2);
	    double t3 = Math.sin(a1)*Math.sin(b1);
	    double tt = Math.acos(t1 + t2 + t3);
	    System.out.println("Coordinate.gps2m() : "+6366000*tt);
	    return 6366000*tt;
	}
	@Override
	public String toString() {
		return "Latitude : "+latitude+", longitude : "+longitude+", accuracy : "+accuracy+", bearing : "+bearing;
	}
}
